//
//  TopView.m
//  FruitVIP
//
//  Created by 孙艳玲 on 15/5/28.
//  Copyright (c) 2015年 孙艳玲. All rights reserved.
//

#import "TopView.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@implementation TopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSub];
    }
    return self;
}

- (void)createSub
{
    UIImageView *addressImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/2 - 0.5,44)];
    addressImage.backgroundColor = [UIColor colorWithRed:109.0/255.0 green:77.0 / 255.0 blue:69 / 255.0 alpha:1];
    addressImage.userInteractionEnabled = YES;
    
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(addressImage.frame.size.width / 3 , 5, addressImage.frame.size.width / 3, addressImage.frame.size.height / 3)];
    addressLabel.text = @"提货点";
    addressLabel.textColor = [UIColor whiteColor];
    addressLabel.font = [UIFont systemFontOfSize:14];
//    addressLabel.textColor = [self colorWithHexString:@"6f4f47"];
    [addressImage addSubview:addressLabel];
    
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(addressImage.frame.size.width / 5, addressLabel.frame.origin.y + addressLabel.frame.size.height, 4 * addressImage.frame.size.width / 5, 20)];
    detailLabel.text = @"上海金钟路提货点";
    detailLabel.font = [UIFont systemFontOfSize:12];
//    detailLabel.textColor = [self colorWithHexString:@"6f4f47"];
    detailLabel.textColor = [UIColor whiteColor];
    [addressImage addSubview:detailLabel];
    [self addSubview:addressImage];
    
    UITapGestureRecognizer *addressTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addressTapAction)];
    [addressImage addGestureRecognizer:addressTap];
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(addressImage.frame.origin.x + addressImage.frame.size.width, addressImage.frame.origin.y, 0.5, addressImage.frame.size.height)];
//    lineLabel.backgroundColor = [UIColor colorWithRed:184.0/255.0 green:184.0/255.0 blue:184.0/255.0 alpha:0.1];
    lineLabel.backgroundColor = [UIColor grayColor];
    [self addSubview:lineLabel];
    
    
    UIImageView *sortImage = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2 , 44)];
    sortImage.backgroundColor = [UIColor colorWithRed:110.0/255.0 green:78.0 / 255.0 blue:71 / 255.0 alpha:1];
    sortImage.userInteractionEnabled = YES;
    
    
    UILabel *sortLabel = [[UILabel alloc] initWithFrame:CGRectMake(sortImage.frame.size.width / 3, 5, sortImage.frame.size.width / 3,  sortImage.frame.size.height / 3)];
    sortLabel.text = @"热销";
//     sortLabel.textColor = [self colorWithHexString:@"6f4f47"];
    sortLabel.textColor = [UIColor whiteColor];
    sortLabel.textAlignment = NSTextAlignmentCenter;
    sortLabel.font = [UIFont systemFontOfSize:14];
    [sortImage addSubview:sortLabel];
    
    UILabel *allLabel = [[UILabel alloc] initWithFrame:CGRectMake(sortImage.frame.size.width / 3, sortLabel.frame.origin.y + sortLabel.frame.size.height,  sortImage.frame.size.width / 3, 20)];
    allLabel.text = @"分类";
    allLabel.font = [UIFont systemFontOfSize:12];
    allLabel.textAlignment = NSTextAlignmentCenter;
    allLabel.textColor = [self colorWithHexString:@"6f4f47"];
    allLabel.textColor = [UIColor whiteColor];
    [sortImage addSubview:allLabel];
    [self addSubview:sortImage];
    
    UITapGestureRecognizer *sortTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sortTapAction)];
    [sortImage addGestureRecognizer:sortTap];
    

}

-(UIColor*)colorWithHexString:(NSString*)hex
{
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor grayColor];
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    
    if ([cString length] != 6) return  [UIColor grayColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}
- (void)addressTapAction
{
    [self.myDetailDelegate detailAddress];
}

- (void)sortTapAction
{
    [self.myFruitDelegate allFruit];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
